/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.presentationorganization;

import org.openaecfm.model.geometry.Axis2Placement3D;
import org.openaecfm.model.presentationappearance.ColourRgb;
import org.openaecfm.model.presentationorganization.LightEmissionSourceEnum;
import org.openaecfm.model.presentationorganization.LightSource;
import org.openaecfm.model.presentationorganization.LightSourceGoniometric;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class LightSourceGoniometricIO extends
		AbstractEntityInstanceInputImpl<LightSourceGoniometric> implements
		IEntityInstanceOutput<LightSourceGoniometric> {
	private IEntityInstanceUtilInput<LightSource> utilInput;
	private IEntityInstanceUtilOutput<LightSource> utilOutput;

	public void setUtilOutput(IEntityInstanceUtilOutput<LightSource> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(LightSourceGoniometric pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCLIGHTSOURCEGONIOMETRIC", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set Position
		ei.addParameter(new EntityInstanceNameParameter(appDataManager
				.getName(pojo.getPosition())));

		// Set ColourAppearance
		if (pojo.getColourAppearance() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getColourAppearance())));
		}

		// Set ColourTemperature
		ei.addParameter(new RealParameter(pojo.getColourTemperature()));

		// Set LuminousFlux
		ei.addParameter(new RealParameter(pojo.getLuminousFlux()));

		// Set LightEmissionSource
		ei.addParameter(new EnumerationParameter(pojo.getLightEmissionSource()));

		// Set LightDistributionDataSource
		ei.addParameter(new EntityInstanceNameParameter(appDataManager
				.getName(pojo.getLightDistributionDataSource())));

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected LightSourceGoniometric doCreateObject() {
		return new LightSourceGoniometric();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			LightSourceGoniometric pojo, IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Position
		EntityInstanceNameParameter position = (EntityInstanceNameParameter) sei
				.getParameter(4);
		pojo.setPosition((Axis2Placement3D) appDataManager.getObject(position
				.toInstanceName()));

		// Set ColourAppearance
		Parameter colourapp = sei.getParameter(5);
		if (colourapp.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) colourapp;
			pojo.setColourAppearance((ColourRgb) appDataManager.getObject(n
					.toInstanceName()));
		}

		// Set LightDistributionDataSource
		EntityInstanceNameParameter lightdis = (EntityInstanceNameParameter) sei
				.getParameter(9);
		Object entity = appDataManager.getObject(lightdis.toInstanceName());
		pojo.setLightDistributionDataSource(entity);
	}

	public void setUtilInput(IEntityInstanceUtilInput<LightSource> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			LightSourceGoniometric pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set ColourTemperature
		RealParameter colourtem = (RealParameter) sei.getParameter(6);
		pojo.setColourTemperature(colourtem.toDouble());

		// Set LuminousFlux
		RealParameter luminous = (RealParameter) sei.getParameter(7);
		pojo.setLuminousFlux(luminous.toDouble());

		// Set LightEmissionSource
		EnumerationParameter lightemi = (EnumerationParameter) sei
				.getParameter(8);
		pojo.setLightEmissionSource(LightEmissionSourceEnum.valueOf(lightemi
				.toEnumString()));
	}
}
